package com.wsc.wisdomchina.ai.widget;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.bumptech.glide.Glide;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.ai.widget.photoview.PhotoView;
import com.wsc.wisdomchina.ai.widget.photoview.PhotoViewAttacher;


/**
 * 图片操作弹出框
 *
 * @author 沈宇坤
 * @time 16.10.28
 */
public class ImageZoomPop extends PopupWindow implements PhotoViewAttacher.ClickCloseListener {
	/** 界面布局 */
	private View mContentView = null;
	/** 上下文菜单 */
	private Context mContext = null;
	/** 可放大缩小的ImageView */
	private PhotoView mImageView = null;
	/** 要显示的图片地址 */
	private String mUrl = "";
	private PhotoViewAttacher mAttacher = null;

	/**
	 * 构造函数
	 *
	 * @param
	 *
	 * @param context
	 *            上下文
	 * @param mUrl
	 *            图片地址
	 */
	public ImageZoomPop(Context context, String mUrl) {
		// 解决4.2以下低版本空指针问题
		super(context);
		this.mContext = context;
		this.mUrl = mUrl;
		// 初始化布局
		initView();
		// 设置填充布局
		this.setContentView(mContentView);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.black));
		this.setBackgroundDrawable(dw);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.c_anim_move_bt);
		this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		// 布局
		ViewGroup nullParent = null;
		mContentView = LayoutInflater.from(mContext).inflate(R.layout.layout_image_zoom, nullParent);
		// 要显示图片的ImageView
		mImageView = (PhotoView) mContentView.findViewById(R.id.itemImage);
		mAttacher = new PhotoViewAttacher(mImageView);
		mAttacher.setClickCloseListener(this);
		// 显示图片
		Glide.with(mContext).load(mUrl).into(mImageView);
		mAttacher.update();
	}

	/**
	 * 显示PopWindow
	 */
	public void showPopWindow() {
		this.showAtLocation(mContentView.getRootView(), Gravity.CENTER, 0, 0);
	}

	/**
	 * 关闭PopWindow
	 */
	public void closePopupWindow() {
		if (ImageZoomPop.this.isShowing()) {
			ImageZoomPop.this.dismiss();
		}
	}

	@Override
	public void close() {
		this.dismiss();
	}
}
